

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>pyamp.ui.qt.styleSheet &mdash; pyamp v1.2 documentation</title>
    <link rel="stylesheet" href="../../../../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../../../../_static/pygments.css" type="text/css" />
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../../../../',
        VERSION:     '1.2',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../../../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../../../_static/doctools.js"></script>
    <link rel="top" title="pyamp v1.2 documentation" href="../../../../index.html" />
    <link rel="up" title="pyamp.ui.qt" href="../qt.html" /> 
  </head>
  <body>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../../../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../../../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li><a href="../../../../index.html">pyamp v1.2 documentation</a> &raquo;</li>
          <li><a href="../../../index.html" >Module code</a> &raquo;</li>
          <li><a href="../qt.html" accesskey="U">pyamp.ui.qt</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <h1>Source code for pyamp.ui.qt.styleSheet</h1><div class="highlight"><pre>
<span class="c"># Copyright 2012 Brett Ponsler</span>
<span class="c"># This file is part of pyamp.</span>
<span class="c">#</span>
<span class="c"># pyamp is free software: you can redistribute it and/or modify</span>
<span class="c"># it under the terms of the GNU General Public License as published by</span>
<span class="c"># the Free Software Foundation, either version 3 of the License, or</span>
<span class="c"># (at your option) any later version.</span>
<span class="c">#</span>
<span class="c"># pyamp is distributed in the hope that it will be useful,</span>
<span class="c"># but WITHOUT ANY WARRANTY; without even the implied warranty of</span>
<span class="c"># MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the</span>
<span class="c"># GNU General Public License for more details.</span>
<span class="c">#</span>
<span class="c"># You should have received a copy of the GNU General Public License</span>
<span class="c"># along with pyamp.  If not, see &lt;http://www.gnu.org/licenses/&gt;.</span>
<span class="sd">&#39;&#39;&#39;The styleSheet module contains the StyleSheet class which provides the</span>
<span class="sd">ability to more easily create a string for setting the style sheet of a</span>
<span class="sd">QWidget.</span>

<span class="sd">&#39;&#39;&#39;</span>


<div class="viewcode-block" id="StyleSheet"><a class="viewcode-back" href="../../../../_generated/pyamp_ui_qt_styleSheet.html#pyamp.ui.qt.styleSheet.StyleSheet">[docs]</a><span class="k">class</span> <span class="nc">StyleSheet</span><span class="p">:</span>
    <span class="sd">&#39;&#39;&#39;The StyleSheet class provides a wrapper for :class:`PyQt4.QtGui.QWidget`</span>
<span class="sd">    style sheet strings. StyleSheets can be easily created an exported into a</span>
<span class="sd">    string that is properly formatted for setting the style sheet of a</span>
<span class="sd">    :class:`PyQt4.QtGui.QWidget`.</span>

<span class="sd">    Example::</span>

<span class="sd">        from PyQt4.QtGui import QApplication, QWidget</span>
<span class="sd">        from pyamp.ui.qt import StyleSheet</span>

<span class="sd">        # Create the widget, and set its name</span>
<span class="sd">        app = QApplication([])</span>
<span class="sd">        widget = QWidget()</span>
<span class="sd">        widget.setObjectName(&quot;ObjectName&quot;)</span>

<span class="sd">        # Create a simple style sheet</span>
<span class="sd">        style = StyleSheet(border=&quot;2px solid black&quot;)</span>
<span class="sd">        normal = &quot;border: 2px solid black&quot;</span>

<span class="sd">        # These calls are identical</span>
<span class="sd">        widget.setStyleSheet(str(style))</span>
<span class="sd">        widget.setStyleSheet(normal)</span>

<span class="sd">        # Create another style sheet, notice that underscores in the</span>
<span class="sd">        # StylSheet parameter names are converted to dashes in the</span>
<span class="sd">        # style sheet string</span>
<span class="sd">        style = StyleSheet(background_color=&quot;red&quot;, color=&quot;blue&quot;,</span>
<span class="sd">                           font_weight=&quot;bold&quot;)</span>
<span class="sd">        normal = &quot;background-color: red; color: blue; font-weight: bold&quot;</span>

<span class="sd">        # These calls are identical</span>
<span class="sd">        widget.setStyleSheet(str(style))</span>
<span class="sd">        widget.setStyleSheet(normal)</span>

<span class="sd">        # You can also specify a specific CSS selector string, which can be</span>
<span class="sd">        # either a specific string, or a widget. If given a widget, the</span>
<span class="sd">        # selector will be created using the object&#39;s class name (e.g.,</span>
<span class="sd">        # QWidget) and will append an ID selector with the object&#39;s name (if</span>
<span class="sd">        # and only if the widget is named).</span>
<span class="sd">        style1 = StyleSheet(widget=&quot;QWidget#ObjectName&quot;,</span>
<span class="sd">                            border=&quot;1px solid red&quot;, margin_top=&quot;10&quot;)</span>
<span class="sd">        style2 = StyleSheet(widget, border=&quot;1px solid red&quot;, margin_top=&quot;10&quot;)</span>
<span class="sd">        normal = &quot;QWidget#ObjectName { border: 1px solid red; margin-top: 10;&quot;</span>

<span class="sd">        # These calls are identical</span>
<span class="sd">        widget.setStyleSheet(str(style1))</span>
<span class="sd">        widget.setStyleSheet(str(style2))</span>
<span class="sd">        widget.setStyleSheet(normal)</span>

<span class="sd">    &#39;&#39;&#39;</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">widget</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;</span>
<span class="sd">        * widget -- The widget selector for this style sheet. Can be either a</span>
<span class="sd">                    string, or an actual widget object</span>
<span class="sd">        * kwargs -- The keyword arguments</span>

<span class="sd">        &#39;&#39;&#39;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">__widget</span> <span class="o">=</span> <span class="n">widget</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">__properties</span> <span class="o">=</span> <span class="n">kwargs</span>

    <span class="k">def</span> <span class="nf">__str__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;Convert the StyleSheet into a string.&#39;&#39;&#39;</span>
        <span class="n">itemStr</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">__propertiesToString</span><span class="p">()</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">__widget</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span>
            <span class="n">itemStr</span> <span class="o">=</span> <span class="s">&quot;</span><span class="si">%s</span><span class="s"> { </span><span class="si">%s</span><span class="s"> }&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">__getWidgetSelector</span><span class="p">(),</span> <span class="n">itemStr</span><span class="p">)</span>

        <span class="k">return</span> <span class="n">itemStr</span>

    <span class="k">def</span> <span class="nf">__propertiesToString</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;Convert the given properties for this StyleSheet into a string that</span>
<span class="sd">        is usable to set the style sheet of a QWidget.</span>

<span class="sd">        &#39;&#39;&#39;</span>
        <span class="n">items</span> <span class="o">=</span> <span class="nb">map</span><span class="p">(</span><span class="k">lambda</span> <span class="n">i</span><span class="p">:</span> <span class="s">&quot;</span><span class="si">%s</span><span class="s">: </span><span class="si">%s</span><span class="s">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">__fixName</span><span class="p">(</span><span class="n">i</span><span class="p">[</span><span class="mi">0</span><span class="p">]),</span> <span class="n">i</span><span class="p">[</span><span class="mi">1</span><span class="p">]),</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">__properties</span><span class="o">.</span><span class="n">items</span><span class="p">())</span>
        <span class="k">return</span> <span class="s">&#39;; &#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">items</span><span class="p">)</span>

    <span class="nd">@classmethod</span>
    <span class="k">def</span> <span class="nf">__fixName</span><span class="p">(</span><span class="n">cls</span><span class="p">,</span> <span class="n">name</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;Translate a class argument name to a CSS property name by</span>
<span class="sd">        replacing underscores with dashes.</span>

<span class="sd">        * name -- The name to fix</span>

<span class="sd">        &#39;&#39;&#39;</span>
        <span class="k">return</span> <span class="n">name</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s">&quot;_&quot;</span><span class="p">,</span> <span class="s">&quot;-&quot;</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">__getWidgetSelector</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;Return the CSS selector based on the current widget.&#39;&#39;&#39;</span>
        <span class="k">if</span> <span class="nb">type</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">__widget</span><span class="p">)</span> <span class="o">!=</span> <span class="nb">type</span><span class="p">(</span><span class="nb">str</span><span class="p">()):</span>
            <span class="c"># If the widget is not a string, then use its class name</span>
            <span class="c"># to create the selector</span>
            <span class="n">widgetStr</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">__widget</span><span class="o">.</span><span class="n">__class__</span><span class="o">.</span><span class="n">__name__</span>

            <span class="c"># If the widget also has a valid object name then apply the</span>
            <span class="c"># id selector for the object name to the selector</span>
            <span class="n">objectName</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">__widget</span><span class="o">.</span><span class="n">objectName</span><span class="p">()</span>
            <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">objectName</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">:</span>
                <span class="n">widgetStr</span> <span class="o">+=</span> <span class="s">&quot;#</span><span class="si">%s</span><span class="s">&quot;</span> <span class="o">%</span> <span class="n">objectName</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="c"># If the widget is just a string, then we should use that as</span>
            <span class="c"># the selector</span>
            <span class="n">widgetStr</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">__widget</span>

        <span class="k">return</span> <span class="n">widgetStr</span></div>
</pre></div>

          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
<div id="searchbox" style="display: none">
  <h3>Quick search</h3>
    <form class="search" action="../../../../search.html" method="get">
      <input type="text" name="q" size="18" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../../../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../../../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li><a href="../../../../index.html">pyamp v1.2 documentation</a> &raquo;</li>
          <li><a href="../../../index.html" >Module code</a> &raquo;</li>
          <li><a href="../qt.html" >pyamp.ui.qt</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
        &copy; Copyright 2012, Brett Ponsler.
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.0.1.
    </div>
  </body>
</html>